package com.jianguozh.difs.common.constant;

import com.jianguozh.difs.common.bean.IErrorCode;

/**
 * REST API 错误码
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/3/9 14:09
 */
public enum ApiErrorCode implements IErrorCode {

    /**
     * 内务异常
     */
    INTERNAL_ERROR(500, "内部异常"),
    DATABASE_ERROR(501, "数据库异常"),
    BAD_REQUEST(502, "非法参数"),
    BAD_CONTENT_TYPE(50201, "Content type 不支持"),

    UNAUTHORIZED(401, "未授权"),
    CREDENTIALS_EXPIRED(40110, "凭证过期"),
    CLIENT_UNSUPPORT(40102, "客户端未授权"),
    BADCREDENTIALS(40103, "账号密码错误"),
    METHOD_NOT_ALLOWED(40104, "不允许请求"),

    ACCESS_DENIED(403, "不允许访问"),

    /**
     * 失败
     */
    FAILED(-1, "操作失败"),
    /**
     * 成功
     */
    SUCCESS(0, "执行成功");

    private final long code;
    private final String msg;

    ApiErrorCode(final long code, final String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ApiErrorCode fromCode(long code) {
        ApiErrorCode[] ecs = ApiErrorCode.values();
        for (ApiErrorCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return SUCCESS;
    }


    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, msg=%s} ", code, msg);
    }

    public long getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

}
